package com.yc.servlet;

import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * @author: zh
 * @date: 2022/3/10 15:33
 * @version: 1.0
 */
public class BasicServlet extends HttpServlet {
    public void send(HttpServletResponse resp, int result) {
        //向前端发送字符串数据
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(result);
    }

    public void send(HttpServletResponse resp, String result) {
        //向前端发送字符串数据
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(result);
    }

    public void send(HttpServletResponse resp, Object result) {
        //向前端发送字符串数据
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(result);
    }

    public void send(HttpServletResponse resp, List<Map<String, String>> list) {
        //向前端发送字符串数据
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
            Gson gson = new Gson();
            String s = gson.toJson(list);
            writer.print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
